package com.example.workmode.Timing;

import com.example.workmode.MainActivity;
import com.example.workmode.R;
import com.example.workmode.R.drawable;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class WorkTimer extends Service {

	int mId = 123;
	long duration;
	SharedPreferences pref;
	String time = "00:00";
	String content = "Remaining Work Duration";
	
	Timer timer;
	
	@Override 
	public void onCreate() {
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		pref = getSharedPreferences("MyPrefs", 0);
		duration = (long) pref.getInt("workDuration", 1) * 60 * 1000;
		timer = new Timer(duration, 1000);		
		timer.start();
		getApplicationContext().getApplicationContext().sendBroadcast(
                new Intent("StartupReceiver_Manual_Start"));
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		timer.cancel();
		super.onDestroy();
	}
	
	private class Timer extends CountDownTimer {
		
		public Timer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			time = getTime(millisUntilFinished/1000);
			sendNotification();
			getApplicationContext().getApplicationContext().sendBroadcast(
	                new Intent("StartupReceiver_Manual_Start"));
			Intent broadcastintent = new Intent("showTime");
			broadcastintent.putExtra("timer", time);
			broadcastintent.putExtra("state", content);
			sendBroadcast(broadcastintent);
		}

		@Override
		public void onFinish() {
			time = "0:00";
			sendNotification();
			Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
			v.vibrate(500);
			Intent broadcastintent = new Intent("showTime");
			broadcastintent.putExtra("timer", time);
			sendBroadcast(broadcastintent);
			Intent trIntent = new Intent();
     		trIntent.setClass(getApplicationContext(), PromptToStartBreak.class);
     		trIntent.putExtra("state", "workTimeUp");
     		trIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        			
     		getApplicationContext().startActivity(trIntent);
		}
		
		private void sendNotification() {
			NotificationCompat.Builder mBuilder =
			        new NotificationCompat.Builder(getApplicationContext())
			        .setSmallIcon(R.drawable.ic_launcher)
			        .setContentTitle(time)
			        .setContentText(content);

			/*Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);

			TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
			stackBuilder.addParentStack(MainActivity.class);
			stackBuilder.addNextIntent(resultIntent);
			PendingIntent resultPendingIntent =
			        stackBuilder.getPendingIntent(
			            0,
			            PendingIntent.FLAG_UPDATE_CURRENT
			        );
			mBuilder.setContentIntent(resultPendingIntent); */
			NotificationManager mNotificationManager =
			    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			mNotificationManager.notify(mId, mBuilder.build());
		}
		
		private String getTime(long time) {
			long minutes = time/60;
			long seconds = time%60;
			if (seconds < 10) {
				return String.valueOf(minutes) + ":0" + String.valueOf(seconds);
			} else {
				return String.valueOf(minutes) + ":" + String.valueOf(seconds);
			}
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}
